package com.bjwl.web.contrlller.branchstore;

import com.bjwl.pojo.*;
import com.bjwl.pojo.dto.OrdersBrDto;
import com.bjwl.pojo.vo.BsPageVoOrders;
import com.bjwl.service.DailyService;
import com.bjwl.service.FindBaseService;
import com.bjwl.service.OrdersService;
import com.bjwl.utils.PageBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.datetime.DateFormatter;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: czc
 * @Date:Create：in 2020/4/15 13:33
 * @Description:订单
 * @Modified By：
 * @Version: 1.0
 */
@Controller
@RequestMapping("/Or")
@SessionAttributes("pagefind")
public class OrdersController {

    @Autowired
    private OrdersService ordersService;
    @Autowired
    private DailyService dailyService;
    @Autowired
    private FindBaseService findBaseService;




    //根据当前管理员的分店id查询派单信息
    @RequestMapping("findOrdersByBstoreid")
    public String findOrdersByBstoreid(HttpSession httpSession, Integer branchstoreid){
        List<Orders>  OrdersList= ordersService.findOrdersByBstoreid(branchstoreid);
        httpSession.setAttribute("OrdersList",OrdersList);
        return "branch_oa/works-list";
    }

    @RequestMapping("/updateShowOrders")
    public String updateShowOrders(HttpSession httpSession, Integer oid){
        Bsadmin bsadmin = (Bsadmin) httpSession.getAttribute("bsadmin1");
        Orders orders = ordersService.updateShowOrders(oid);
        httpSession.setAttribute("orders",orders);
        List<Partment> partments = findBaseService.findAllPartment(bsadmin.getBsid());
        List<ServiceClass> serviceClasss = findBaseService.findAllServiceClass(bsadmin.getBsid());
        List<ServiceProject> serviceProjects = findBaseService.findAllServiceProject(bsadmin.getBsid());
        List<ServiceSource> serviceSources = findBaseService.findAllServiceSource(bsadmin.getBsid());
        httpSession.setAttribute("partmentss",partments);
        httpSession.setAttribute("serviceClasss",serviceClasss);
        httpSession.setAttribute("serviceProjects",serviceProjects);
        httpSession.setAttribute("serviceSources",serviceSources);
        return "branch_oa/works-edit";
    }

    //根据当前id进行业务信息修改
    @RequestMapping("/updateOrdersByid")
    public @ResponseBody Boolean updateOrdersByid(Orders orders){
        ordersService.updateShowOrders(orders);



        return true;
    }

    //根据oid删除当前派单信息
    @RequestMapping("/deleteOrders")
    public @ResponseBody Boolean deleteOrders(Integer oid){
        Orders orders = ordersService.updateShowOrders(oid);


        ordersService.deleteOrders(oid);

        return true;
    }

    //添加派单
    @RequestMapping("/addOrders")
    public @ResponseBody Boolean addOrders(Orders orders) {


        Date dd=new Date();
        //格式化
        SimpleDateFormat sim=new SimpleDateFormat("yyyyMMddHHmmss");
        String oWriteTime=sim.format(dd);

        orders.setOrstate(0);//派单状态0未派单，1已派单
        orders.setoWriteTime(new Date());//创建单子时间
        orders.setSerialnumber("LS-"+oWriteTime); //流水号
        ordersService.addOrders(orders);

        return true;
    }


    @InitBinder
    public void initDateFormate(WebDataBinder dataBinder) {
        dataBinder.addCustomFormatter(new DateFormatter("yyyy-MM-dd"));
    }


    public Integer finOrdersCount(Integer bsid){
          Integer ordersCount = ordersService.finOrdersCount(bsid);
          return ordersCount;

    }
    //********************************
    @RequestMapping("/refreshWork")
    public String refreshWork(HttpSession httpSession,HttpServletRequest request, Integer branchstoreid, Integer pageNum, Integer orstate , Integer orderid, String ordervalue , String wstarttime , String wendtime, String servproject, Model model) {
        httpSession.removeAttribute("bsPageVoOrders1");
        httpSession.removeAttribute("bsPageVoOrders");

        if(pageNum == null){
            pageNum =1;

        }

        Bsadmin bsadmin1 = (Bsadmin) httpSession.getAttribute("bsadmin1");
        branchstoreid = bsadmin1.getBsid();
        PageBean<Orders> orderList = ordersService.findWorkByBstoreid(pageNum,branchstoreid,orstate,orderid, ordervalue,wstarttime,wendtime,servproject);
        request.setAttribute("orderList", orderList);
        OrdersBrDto ordersBrDto = new OrdersBrDto(orstate,orderid,ordervalue,wstarttime,wendtime,servproject);
        request.setAttribute("ordersBrDto",ordersBrDto);
        List<ServiceProject> serviceProjects =  findBaseService.findAllServiceProject(branchstoreid);
        httpSession.setAttribute("serviceProjects",serviceProjects);
        return "branch_oa/works-list";

    }
    //********************************
   /* @RequestMapping("/refreshWork")
    public String refreshWork(HttpSession httpSession, Integer branchstoreid, Integer pageNum, Integer orstate , Integer orderid, String ordervalue , String wstarttime , String wendtime, String servproject, Model model) {
        httpSession.removeAttribute("bsPageVoOrders1");
        httpSession.removeAttribute("bsPageVoOrders");

        if(pageNum == null){
            pageNum =1;

        }
        List<ServiceProject> serviceProjects =  findBaseService.findAllServiceProject(branchstoreid);
        httpSession.setAttribute("serviceProjects",serviceProjects);
        Bsadmin bsadmin1 = (Bsadmin) httpSession.getAttribute("bsadmin1");
        branchstoreid = bsadmin1.getBsid();
        PageBean<Orders> orderList = ordersService.findWorkByBstoreid(pageNum,branchstoreid,orstate,orderid, ordervalue,wstarttime,wendtime,servproject);
        httpSession.setAttribute("orderList", orderList);



        return "branch_oa/works-list";

    }*/
    //********************************



  /*  @RequestMapping("/findWorkByBstoreid")
    public String findWorkByBstoreid(HttpSession httpSession, Integer branchstoreid, Integer pageNum, Integer orstate , Integer orderid, String ordervalue , String wstarttime , String wendtime, String servproject) {
        List<ServiceProject> serviceProjects =  findBaseService.findAllServiceProject(branchstoreid);
        httpSession.setAttribute("serviceProjects",serviceProjects);
        Bsadmin bsadmin1 = (Bsadmin) httpSession.getAttribute("bsadmin1");
        BsPageVoOrders bsPageVoOrders1 = (BsPageVoOrders) httpSession.getAttribute("bsPageVoOrders");

        if(pageNum == null ){
            pageNum =1;
        }

        if(bsPageVoOrders1==null){


            branchstoreid = bsadmin1.getBsid();
            PageBean<Orders> orderList = ordersService.findWorkByBstoreid(pageNum,branchstoreid,orstate,orderid, ordervalue,wstarttime,wendtime,servproject);
            httpSession.setAttribute("orderList", orderList);
            httpSession.removeAttribute("bsPageVoOrders1");
        }else {
            httpSession.getAttribute("serviceProjects");
            branchstoreid = bsadmin1.getBsid();
            PageBean<Orders> orderList = ordersService.findWorkByBstoreid(pageNum,branchstoreid,bsPageVoOrders1.getOrstate(),
                    bsPageVoOrders1.getOrderid(), bsPageVoOrders1.getOrdervalue(),bsPageVoOrders1.getWstarttime(),bsPageVoOrders1.getWendtime(),
                    bsPageVoOrders1.getServproject());
            httpSession.setAttribute("orderList", orderList);

        }

        if(orstate!=null || orderid!=null || ordervalue!=null || servproject!=null || wendtime!=null || wstarttime!=null){
            BsPageVoOrders bsPageVoOrders = new BsPageVoOrders();
            bsPageVoOrders.setOrderid(orderid);
            bsPageVoOrders.setOrdervalue(ordervalue);
            bsPageVoOrders.setOrstate(orstate);
            bsPageVoOrders.setServproject(servproject);
            bsPageVoOrders.setWendtime(wendtime);
            bsPageVoOrders.setWstarttime(wstarttime);
            httpSession.setAttribute("bsPageVoOrders",bsPageVoOrders);
        }

        return "branch_oa/works-list";

    }
*/

    //**********
    @RequestMapping("junmpAddOrders")
    public String junmpAddOrders(HttpSession httpSession,Integer buid){
        Business businessaaa = ordersService.addOrdersShowbusin(buid);
        httpSession.setAttribute("businessaaa",businessaaa);
        Bsadmin bsadmin = (Bsadmin) httpSession.getAttribute("bsadmin1");
        List<Partment> apartments = findBaseService.findAllPartment(bsadmin.getBsid());
        List<ServiceClass> aserviceClasss = findBaseService.findAllServiceClass(bsadmin.getBsid());
        List<ServiceProject> aserviceProjects = findBaseService.findAllServiceProject(bsadmin.getBsid());
        List<ServiceSource> aserviceSources = findBaseService.findAllServiceSource(bsadmin.getBsid());
        httpSession.setAttribute("apartmentss",apartments);
        httpSession.setAttribute("aserviceClasss",aserviceClasss);
        httpSession.setAttribute("aserviceProjects",aserviceProjects);
        httpSession.setAttribute("aserviceSources",aserviceSources);
        if(businessaaa.getBsstate()==0){
            return "branch_oa/works-addBus";
        }else {
            return "branch_oa/works-addCl";
        }

    }

    @RequestMapping("junmpAddOrders1")
    public String junmpAddOrders1(HttpSession httpSession){
        httpSession.removeAttribute("businessaaa");
        Bsadmin bsadmin = (Bsadmin) httpSession.getAttribute("bsadmin1");
        List<Partment> apartments = findBaseService.findAllPartment(bsadmin.getBsid());
        List<ServiceClass> aserviceClasss = findBaseService.findAllServiceClass(bsadmin.getBsid());
        List<ServiceProject> aserviceProjects = findBaseService.findAllServiceProject(bsadmin.getBsid());
        List<ServiceSource> aserviceSources = findBaseService.findAllServiceSource(bsadmin.getBsid());
        httpSession.setAttribute("apartmentss",apartments);
        httpSession.setAttribute("aserviceClasss",aserviceClasss);
        httpSession.setAttribute("aserviceProjects",aserviceProjects);
        httpSession.setAttribute("aserviceSources",aserviceSources);
        return "branch_oa/works-addOr";
    }



    @RequestMapping("/updateOrstate")
    public @ResponseBody Boolean updateOrstate(@RequestBody Orders orders){
        ordersService.updateOrstate(orders);
        return true;
    }

}
